﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Obra.Sync;
using Obra.Common;

namespace Obra.Test
{
    [TestFixture]
    public class FileSystemServiceProviderTester
        : NMockTester
    {
        private string testDirectory;

        [SetUp]
        public override void SetUp()
        {
            base.SetUp();
            this.testDirectory = TestHelper.CreateTempDirectory();
        }

        [TearDown]
        public override void TearDown()
        {
            base.TearDown();
            Directory.Delete(this.testDirectory, true/*recursive*/);
        }

        [Test]
        public void MediaFilesOnly()
        {
            string file1 = TestHelper.CreateRandomFileWithExtension(
                this.testDirectory,
                ".bmp");

            FileSystemServiceProvider fs = new FileSystemServiceProvider();

            var files = fs.GetMediaFiles(
                this.testDirectory);

            Assert.AreEqual(0, files.Count);
        }

        [Test]
        public void NestedMediaFiles()
        {
            string directoryA = Path.Combine(
                this.testDirectory,
                Path.GetRandomFileName());

            Directory.CreateDirectory(directoryA);

            string directoryB = Path.Combine(
                this.testDirectory,
                Path.GetRandomFileName());

            Directory.CreateDirectory(directoryB);

            string fileName1 = TestHelper.CreateRandomFileWithExtension(directoryA, ".avi");
            string fileName2 = TestHelper.CreateRandomFileWithExtension(directoryA, ".gif");
            string fileName3 = TestHelper.CreateRandomFileWithExtension(directoryA, ".jpeg");
            string fileName4 = TestHelper.CreateRandomFileWithExtension(directoryA, ".jpg");
            string fileName5 = TestHelper.CreateRandomFileWithExtension(directoryA, ".m4a");
            string fileName6 = TestHelper.CreateRandomFileWithExtension(directoryA, ".m4v");
            string fileName7 = TestHelper.CreateRandomFileWithExtension(directoryA, ".mov");
            string fileName8 = TestHelper.CreateRandomFileWithExtension(directoryA, ".mpeg");
            string fileName9 = TestHelper.CreateRandomFileWithExtension(directoryA, ".mpg");
            string fileName10 = TestHelper.CreateRandomFileWithExtension(directoryB, ".png");

            FileSystemServiceProvider fs = new FileSystemServiceProvider();

            List<FileInfo> files = fs.GetMediaFiles(
                this.testDirectory);

            Assert.AreEqual(10, files.Count);

            files.Single(x => x.Name.EndsWith(".avi"));
            files.Single(x => x.Name.EndsWith(".gif"));
            files.Single(x => x.Name.EndsWith(".jpeg"));
            files.Single(x => x.Name.EndsWith(".jpg"));
            files.Single(x => x.Name.EndsWith(".m4a"));
            files.Single(x => x.Name.EndsWith(".m4v"));
            files.Single(x => x.Name.EndsWith(".mov"));
            files.Single(x => x.Name.EndsWith(".mpeg"));
            files.Single(x => x.Name.EndsWith(".mpg"));
            files.Single(x => x.Name.EndsWith(".png"));
        }


        [Test]
        public void HalfMediaFiles()
        {
            string directoryA = Path.Combine(
                this.testDirectory,
                Path.GetRandomFileName());

            Directory.CreateDirectory(directoryA);

            string file1 = TestHelper.CreateRandomFileWithExtension(directoryA, ".txt");
            string file2 = TestHelper.CreateRandomFileWithExtension(directoryA, ".png");

            FileSystemServiceProvider fs = new FileSystemServiceProvider();

            var files = fs.GetMediaFiles(
                this.testDirectory);

            Assert.AreEqual(1, files.Count);
            Assert.AreEqual(file2, files[0].FullName);
        }
    }
}
